import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'

const Home = resolve => require(['@/components/Home'], resolve);
import fangRouter from '@/components/Home/router'
import mainRouter from '@/components/Home/router_c'
import { getCookie } from '@/base/utils'
import store from '@/store'

Vue.use(Router);

const router = new Router({
    mode: 'history',
    routes: [{
        path: '/',
        name: 'login',
        component: Login
    }, {
        path: '/home',
        name: 'home',
        component: Home,
        redirect: {
            name: 'welcome'
        },
        children: fangRouter.concat(mainRouter)
    }, ]
});

// 路由跳转前验证
router.beforeEach((to, from, next) => {
   // console.log(to.matched, from.name)
    if (to.matched.length === 0) { //如果未匹配到路由
        next({ name: 'error' }) ; //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
    } else {
        if (getCookie('navName') !== undefined) {
            store.dispatch('setNavName', getCookie('navName'));
        } 
        next();
    }
});
export default router;